﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Models;

namespace ChinaDynastyTours.Views
{
    public partial class VehicleEvaluateForm : DevExpress.XtraEditors.XtraForm
    {
        private DataTable _dtVehicleEvaluateData;
        private string _TourgroupID { get; set; }

        public VehicleEvaluateForm(string tourgroupId)
        {
            InitializeComponent();
            _TourgroupID = tourgroupId;
        }
        private void VehicleEvaluateForm_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_TourgroupID))//如果没传进来团号
            {
                //初始化团下拉列表
                DataSet ds = TeamGroupCtrl.GetAllIDForTeamGroup();
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                        btnTeamGroupID.Properties.Items.Add(dr["TourGroupID"].ToString().Trim());
                }
            }
            else
            {
                btnTeamGroupID.Text = _TourgroupID;
                btnTeamGroupID.Enabled = false;
                vehicleGridView.OptionsBehavior.Editable = false;
                vehicleGridControl.DataSource = VehicleEvaluateCtrl.GetVehicleEvaluateData(_TourgroupID);
            }
        }
        private void btnTeamGroupID_SelectedIndexChanged(object sender, EventArgs e)
        {
            _dtVehicleEvaluateData = VehicleEvaluateCtrl.GetVehicleEvaluateData(btnTeamGroupID.Text);

            if (_dtVehicleEvaluateData == null || _dtVehicleEvaluateData.Rows.Count == 0)
            {
                string tourgroupName = TeamGroupCtrl.GetTeamGroupNameById(btnTeamGroupID.Text);

                DataTable tmpDt = VehicleEvaluateCtrl.GetTravelOrderForVehicleEval(btnTeamGroupID.Text).Tables[0];

                foreach (DataRow dr in tmpDt.Rows)
                {
                    VehicleEvaluateEntity entity = new VehicleEvaluateEntity()
                    {
                        TourgroupID = btnTeamGroupID.Text,
                        TourgroupName = tourgroupName,
                        ProviderID = int.Parse(dr["TravelAgencyID"].ToString()),
                        ProviderName = dr["TravelAgencyName"].ToString(),
                        CityName = dr["City"].ToString(),
                        Good = 0,
                        Middle = 0,
                        Common = 0,
                        Bad = 0,
                        Praise = 0,
                        Complain = 0,
                        Remark = "",
                    };
                    VehicleEvaluateCtrl.CreateVehicleEvaluateData(entity);
                }
                _dtVehicleEvaluateData = VehicleEvaluateCtrl.GetVehicleEvaluateData(btnTeamGroupID.Text);
            }
            //加载数据到UI的gride
            vehicleGridControl.DataSource = _dtVehicleEvaluateData;
            _dtVehicleEvaluateData.RowChanged += new DataRowChangeEventHandler(_dtVehicleEvaluateData_RowChanged);
        }

        void _dtVehicleEvaluateData_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Change || e.Action == DataRowAction.ChangeCurrentAndOriginal || e.Action == DataRowAction.ChangeOriginal)
            {
                //string ProviderID = feedbackGridView.GetRowCellValue(feedbackGridView.FocusedRowHandle, "ProviderID").ToString();

                VehicleEvaluateEntity entity = new VehicleEvaluateEntity()
                {
                    EvaluateID = int.Parse(vehicleGridView.GetRowCellValue(vehicleGridView.FocusedRowHandle, "EvaluateID").ToString()),
                    TourgroupID = e.Row["TourgroupID"].ToString(),
                    TourgroupName = e.Row["TourgroupName"].ToString(),
                    CityName = e.Row["CityName"].ToString(),
                    ProviderName = e.Row["ProviderName"].ToString(),
                    ProviderID = int.Parse(e.Row["ProviderID"].ToString()),
                    Good = int.Parse(e.Row["Good"].ToString()),
                    Middle = int.Parse(e.Row["Middle"].ToString()),
                    Common = int.Parse(e.Row["Common"].ToString()),
                    Bad = int.Parse(e.Row["Bad"].ToString()),
                    Praise = int.Parse(e.Row["Praise"].ToString()),
                    Complain = int.Parse(e.Row["Complain"].ToString()),
                    Remark = e.Row["Remark"].ToString()
                };
                VehicleEvaluateCtrl.UpdateVehicleEvaluateData(entity);
            }
        }
    }
}